package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Material;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.Process;
import com.qidianit.mes.pojo.pro.ProductionOrder;
import com.qidianit.mes.pojo.pro.ProductionReport;
import com.qidianit.mes.pojo.pro.WorkflowProcess;
import com.qidianit.mes.pro.mapper.*;
import com.qidianit.mes.pro.service.ProductionReportService;
import com.qidianit.mes.sys.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @Description:
 * @Author: wang fei long
 * @Date: 2025/9/17 0017 - 16:10
 */
@Service
public class ProductionReportServiceImpl implements ProductionReportService {
    @Autowired
    private ProductionReportMapper productionReportMapper;
    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private ProductionOrderMapper productionOrderMapper;
    @Autowired
    private WorkstationMapper workstationMapper;
    @Autowired
    private WorkflowProcessMapper workflowProcessMapper;
    @Autowired
    private ProcessMapper processMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private UnitMapper unitMapper;
    @Override
    public Result<PageInfo<ProductionReport>> selectProductionReport(ProductionReport productionReport, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<ProductionReport> productionReports = productionReportMapper.selectProductionReport(productionReport);
        for (ProductionReport report : productionReports) {
            report.setTypeDictData(dictDataMapper.selectByType(report.getProductionReportType()));
            report.setStatusDictData(dictDataMapper.selectByStatus(report.getStatus()));
            ProductionOrder productionOrder = productionOrderMapper.selectById(report.getProductionOrderId());
            report.setProductionOrder(productionOrder);
            Material material = materialMapper.selectByCodeMaterial(productionOrder.getMaterialCode());
            productionOrder.setMaterial(material);
            material.setUnit(unitMapper.selectByIdUnit(material.getUnitId()));
            report.setWorkstation(workstationMapper.selectWorkstationByCode(report.getWorkstationCode()));
            WorkflowProcess workflowProcess = workflowProcessMapper.selectById(report.getWorkflowProcessId());
            report.setWorkflowProcess(workflowProcess);

            Process process = processMapper.selectById(report.getWorkflowProcess().getProcessId());
            workflowProcess.setProcess(process);
        }
        return Result.success(new PageInfo<>(productionReports));
    }

    @Override
    public Result updateProductionReport(ProductionReport productionReport) {
        return productionReportMapper.updateProductionReport(productionReport) == 1 ? Result.success() : Result.error("操作失败");
    }

    @Override
    public Result updateStatus1(Long id) {
        return Result.success(productionReportMapper.updateStatus1(id));

    }

    @Override
    public Result updateStatus2(Long id) {
        return Result.success(productionReportMapper.updateStatus2(id));
    }

    @Override
    public Result updateStatus3(Long id) {
        return Result.success(productionReportMapper.updateStatus3(id));
    }

    @Override
    public Result<ProductionReport> selectById(Long id) {
        ProductionReport productionReport = productionReportMapper.selectById(id);
        productionReport.setTypeDictData(dictDataMapper.selectByType(productionReport.getProductionReportType()));

        productionReport.setStatusDictData(dictDataMapper.selectByStatus(productionReport.getStatus()));

        ProductionOrder productionOrder = productionOrderMapper.selectById(productionReport.getProductionOrderId());
        productionReport.setProductionOrder(productionOrder);

        Material material = materialMapper.selectByCodeMaterial(productionOrder.getMaterialCode());
        productionOrder.setMaterial(material);

        material.setUnit(unitMapper.selectByIdUnit(material.getUnitId()));

        productionReport.setWorkstation(workstationMapper.selectWorkstationByCode(productionReport.getWorkstationCode()));

        WorkflowProcess workflowProcess = workflowProcessMapper.selectById(productionReport.getWorkflowProcessId());
        productionReport.setWorkflowProcess(workflowProcess);

        Process process = processMapper.selectById(productionReport.getWorkflowProcess().getProcessId());
        workflowProcess.setProcess(process);
        return Result.success(productionReport);
    }

    @Override
    public Result deleteById(Long id) {
        return productionReportMapper.deleteById(id)==1?Result.success():Result.error("操作失败");
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return productionReportMapper.batchDelete(ids)>0?Result.success():Result.error("操作失败");
    }
}
